<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center
    :z-index="2000" width="800px" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="活动名称" prop="promotion_name"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.promotion_name" placeholder="活动名称" class="w-full" />
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker v-model="form.date" placeholder="日期选择" type="daterange" value-format="YYYY-MM-DD"
            class="w-full" />
        </el-form-item>
        <el-form-item label="活动平台">
          <VSelect v-model="form.platform_id" placeholder="请选择" type="platforms" filterable clearable class="w-full" />
        </el-form-item>
        <el-form-item label="产品分类">
          <VCascader v-model="form.attribute_id" placeholder="请选择" type="2" filterable clearable checkStrictly
            class="w-full" @change="({ labels }) => (form.attribute_name = labels)" />
        </el-form-item>
        <el-form-item label="备注" class="col-span-full">
          <el-input v-model="form.remark" placeholder="备注" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from './api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (pc_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  formRef?.value?.resetFields()
  loadings = reactive({})
  visible.value = true
  if (pc_id) {
    const res = await api.detail({ pc_id })
    form.value = res.data
    const { promotion_start_time, promotion_end_time } = res.data
    form.value.date = [promotion_start_time, promotion_end_time]
  } else {
    form.value = { date: [] }
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  const validator = await formRef.validate()
  if (!validator) return
  loadings.create = true
  const { date } = form.value
  form.value.promotion_start_time = date[0] || ''
  form.value.promotion_end_time = date[1] || ''
  api[form.value.pc_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>